import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:hzy_normal_tool/hzy_normal_tool.dart';

import '../../common/index.dart';
import '../../models/index.dart';

class RootTabWidget extends StatefulWidget {
  const RootTabWidget({
    super.key,
    this.normalIndex = 0,
    required this.itemList,
    required this.onTapIndex,
  });
  final int normalIndex;
  final List itemList;

  final IndexTapCallback onTapIndex;
  @override
  State<StatefulWidget> createState() {
    return RootTabWidgetState();
  }
}

class RootTabWidgetState extends BaseStateWidget<RootTabWidget> {
  int selectIndex = 0;
  @override
  void initState() {
    super.initState();
    selectIndex = widget.normalIndex;
  }

  @override
  Widget createBody({
    required BuildContext context,
    BoxConstraints? constraints,
  }) {
    Widget body = Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        ...widget.itemList.asMap().entries.map(
              (entry) => createItemWidget(
                entry.value,
                entry.key,
              ),
            )
      ],
    );
    body = Container(
      decoration: const BoxDecoration(
        color: Colors.white,
        boxShadow: [
          BoxShadow(
            color: Colors.black12,
            blurRadius: 10,
            spreadRadius: 1,
            offset: Offset(0, -1),
          ),
        ],
      ),
      padding: EdgeInsets.only(
        top: 10,
        bottom: configSafeBottomHeight(normalHeight: 5),
      ),
      child: body,
    );
    return body;
  }

  Widget createItemWidget(
    RootItemModel itemModel,
    int key,
  ) {
    createItemTitleWidget() {
      Widget body = Text(
        itemModel.title ?? '',
        style: TextStyle(
          fontSize: 9,
          color: configSelectColor(
            isSelect: key == selectIndex,
          ),
        ),
      );
      return body;
    }

    createItemImageWidget() {
      Widget body = SizedBox(
        width: 24,
        height: 24,
        child: Icon(
          _getTabIconData(key),
          size: 24,
          color: configSelectColor(
            isSelect: key == selectIndex,
          ),
        ),
      );
      return body;
    }

    Widget body = Column(
      children: [
        createItemImageWidget(),
        const SizedBox(
          height: 3,
        ),
        createItemTitleWidget(),
      ],
    );

    body = InkWell(
      child: body,
      onTap: () {
        setState(() {
          selectIndex = key;
        });
        widget.onTapIndex(key);
      },
    );
    body = Expanded(child: body);
    return body;
  }

  /// 获取底部导航图标数据
  /// [index] 导航索引
  IconData _getTabIconData(int index) {
    switch (index) {
      case 0:
        return Icons.dashboard_outlined;
      case 1:
        return Icons.shopping_cart_outlined;
      case 2:
        return Icons.chat_outlined;
      case 3:
        return Icons.school_outlined;
      case 4:
        return Icons.person_outlined;
      default:
        return Icons.dashboard_outlined;
    }
  }

  Color configSelectColor({
    bool isSelect = false,
  }) {
    return isSelect
        ? context.theme.primaryColor
        : context.theme.unselectedWidgetColor;
  }
}
